|
|
@@ -0,0 +1,91 @@
|
|
1
|
+require "net/http"
|
|
2
|
+require "uri"
|
|
3
|
+
|
|
4
|
+module Agents
|
|
5
|
+ class PushoverPublishAgent < Agent
|
|
6
|
+ cannot_be_scheduled!
|
|
7
|
+
|
|
8
|
+ description <<-MD
|
|
9
|
+The PushoverPublishAgent sends received events to [Pushover](https://pushover.net/) clients on your devices.
|
|
10
|
+
|
|
11
|
+Because Pushover has free limit of 7500 messages per application per month, to use this agent you must [set up](https://pushover.net/apps/build) your own Pushover Application and obtain unique API key.
|
|
12
|
+
|
|
13
|
+Agent needs the following mandatory parameters to work:
|
|
14
|
+
|
|
15
|
+* `api_token` - Pushover API token of application that you have set up
|
|
16
|
+* `user_key` - your User Key
|
|
17
|
+* `message_path` - [JSONPath](http://goessner.net/articles/JsonPath/) to the message text in the event
|
|
18
|
+* `expected_receive_period_in_days` - to the maximum amount of time that you'd expect to pass between Events being consumed by this Agent
|
|
19
|
+
|
|
20
|
+Also you may add the following optional parameters:
|
|
21
|
+
|
|
22
|
+* `device` - your user's device name to send the message directly to that device, rather than all of the user's devices
|
|
23
|
+* `title_path` - [JSONPath](http://goessner.net/articles/JsonPath/) to your message's title, otherwise your registered Pushover app's name will be used
|
|
24
|
+* `url_path` - [JSONPath](http://goessner.net/articles/JsonPath/) to a supplementary URL to show with your message
|
|
25
|
+* `url_title_path` - [JSONPath](http://goessner.net/articles/JsonPath/) to a title for your supplementary URL, otherwise just the URL is shown
|
|
26
|
+* `priority` - set to -1 to always send as a quiet notification, 0 to send as notmal notification, 1 to display as [high-priority](https://pushover.net/api#priority) and bypass the user's quiet hours, or 2 to also require confirmation from the user
|
|
27
|
+* `retry` - if `priority` is set to 2, this parameter should be passed to specify how often (in seconds) the Pushover servers will send the same notification to the user
|
|
28
|
+* `expiry` - if `priority` is set to 2, this parameter should be passed to specify how many seconds your notification will continue to be retried for (every `retry` seconds)
|
|
29
|
+* `sound` - the name of one of the [sounds](https://pushover.net/api#sounds) supported by device clients to override the user's default sound choice
|
|
30
|
+ MD
|
|
31
|
+
|
|
32
|
+ def validate_options
|
|
33
|
+ unless options[:expected_receive_period_in_days].present? &&
|
|
34
|
+ options[:api_token].present? &&
|
|
35
|
+ options[:user_key].present?
|
|
36
|
+ errors.add(:base, "expected_receive_period_in_days, api_token, user_key are required")
|
|
37
|
+ end
|
|
38
|
+ end
|
|
39
|
+
|
|
40
|
+ def working?
|
|
41
|
+ last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
|
|
42
|
+ end
|
|
43
|
+
|
|
44
|
+ def default_options
|
|
45
|
+ {
|
|
46
|
+ :expected_receive_period_in_days => "10",
|
|
47
|
+ :api_token => "",
|
|
48
|
+ :user_key => "",
|
|
49
|
+ :message_path => "text",
|
|
50
|
+ :title_path => "title",
|
|
51
|
+ :url_path => "url",
|
|
52
|
+ :url_title_path => "",
|
|
53
|
+ :priority => "0",
|
|
54
|
+ :sound => "pushover"
|
|
55
|
+ }
|
|
56
|
+ end
|
|
57
|
+
|
|
58
|
+ def receive(events)
|
|
59
|
+ uri = URI("https://api.pushover.net/1/messages.json")
|
|
60
|
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") do |http|
|
|
61
|
+ events.each do |event|
|
|
62
|
+ request = Net::HTTP::Post.new(uri.request_uri)
|
|
63
|
+ post_data = {
|
|
64
|
+ :token => options[:api_token],
|
|
65
|
+ :user => options[:user_key],
|
|
66
|
+ :message => Utils.value_at(event.payload, options[:message_path])
|
|
67
|
+ }
|
|
68
|
+ if options.has_key?(:title_path) then
|
|
69
|
+ post_data[:title] = Utils.value_at(event.payload, options[:title_path])
|
|
70
|
+ end
|
|
71
|
+ if options.has_key?(:url_path) then
|
|
72
|
+ post_data[:url] = Utils.value_at(event.payload, options[:url_path])
|
|
73
|
+ end
|
|
74
|
+ if options.has_key?(:url_title_path) then
|
|
75
|
+ post_data[:url_title] = Utils.value_at(event.payload, options[:url_title_path])
|
|
76
|
+ end
|
|
77
|
+ if options.has_key?(:priority) then
|
|
78
|
+ post_data[:priority] = options[:priority]
|
|
79
|
+ end
|
|
80
|
+ if options.has_key?(:sound) then
|
|
81
|
+ post_data[:sound] = options[:sound]
|
|
82
|
+ end
|
|
83
|
+ request.set_form_data(post_data)
|
|
84
|
+ Rails.logger.debug "PushoverPublishAgent: request: #{request.body}"
|
|
85
|
+ response = http.request(request)
|
|
86
|
+ Rails.logger.debug "PushoverPublishAgent: response: #{response.code} #{response.body}"
|
|
87
|
+ end
|
|
88
|
+ end
|
|
89
|
+ end
|
|
90
|
+ end
|
|
91
|
+end
|